#pragma once
#include <string>
#include <vector>
#include <map>
#include <cstdint>
#include <codecvt>
#include <locale>

#include <iostream>
#include <ft2build.h>
#include FT_FREETYPE_H
#include "../math/vec2.h"

// 修改后的字符结构
struct Character
{
    Vec2 texCoords[4]; // 纹理坐标 (左下, 右下, 右上, 左上)
    uint32_t width;
    uint32_t height;
    int32_t bearingX;
    int32_t bearingY;
    uint32_t advance;
};
class FreetypeMgr
{
private:
    FreetypeMgr() = default;
    ~FreetypeMgr() = default;
    FreetypeMgr(const FreetypeMgr &) = delete;            // 禁用拷贝构造
    FreetypeMgr &operator=(const FreetypeMgr &) = delete; // 禁用赋值操作符

    std::string _fontPath;
    FT_Face _face;
    FT_Library _ft;

    // std::map<std::string, int> _fontMaps;
    std::wstring utf8_to_wstring(const std::string &str);

public:
    static FreetypeMgr *getInstance();
    void init();
    std::string crateFont(const std::string &str, uint32_t fontSize, uint32_t lineHeight);
    void clearFont(std::string key);


    void destroy();

    void saveAtlasAsPGM(const std::string &filename, const std::vector<uint8_t> &atlasData, int width, int height);
    void saveAtlasAsPNG(const std::string &filename, const std::vector<uint8_t> &atlasData, int width, int height);

};
